var express = require('express');
var db = require('../../../tools/database')
var util = require('../../../tools/util')
var router = express.Router();


function changeStatus(itemID, toDelete, onSale, res){
    let sql = ''
    if(toDelete === true){
        sql = 'DELETE FROM Items WHERE ItemID=?'
        db.queryRespond(sql, [itemID], res)
    }else{
        sql = 'UPDATE Items SET OnSale=? WHERE ItemID=?'
        db.queryRespond(sql, [onSale, itemID], res)
    }
}

// 更改商品状态
router.use(function (req, res, next) {
    util.jwtVerify(req, res, (decoded) => {
        let toDelete = req.body.toDelete === 'true'
        let onSale = req.body.onSale === 'true'
        changeStatus(req.body.itemID, toDelete, onSale, res)
    })
});


module.exports = router;